package com.example.demo.lwm2m;

import org.eclipse.leshan.client.californium.LeshanClient;
import org.eclipse.leshan.client.californium.LeshanClientBuilder;
import org.eclipse.leshan.client.object.Device;
import org.eclipse.leshan.client.object.Security;
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.LwM2mId;
import org.eclipse.leshan.core.attributes.AttributeSet;
import org.eclipse.leshan.core.request.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;

/**
 * <pre>
 *  描述:
 * </pre>
 *
 * @author godelgnis(wujiaer)
 * @version 1.0.0
 * @date 2021/12/10 14:20
 */
public class Lwm2mClient {
    public static void main(String[] args) throws InterruptedException {
        Client client = new Client();
        client.start();
//        LeshanClient instance = client.getInstance();
//        instance.getObjectTree().getObjectEnabler(1).writeAttributes(
//                ServerIdentity.SYSTEM, new WriteAttributesRequest(LwM2mId.DEVICE, AttributeSet.parse("test=1&version=pro")));
    }


}
class Client{

    LeshanClient instance;

    LeshanClient getInstance() {
        return instance;
    }

    Client() {}

    void start() {
        String endpoint = "test_client_1" ; // choose an endpoint name
        LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
        ObjectsInitializer initializer = new ObjectsInitializer();
        initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://192.168.114.40:5783", 12345));
        initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, 5 * 60, BindingMode.parse("U"), true, BindingMode.U));
        Device device = new Device("Eclipse Leshan", "model12345", "12345", BindingMode.parse("U"));
        initializer.setInstancesForObject(LwM2mId.DEVICE, device);


// add it to the client
        builder.setObjects(initializer.createAll());
        LeshanClient client = builder.build();
        this.instance = client;
        client.start();
    }
}